package cz.fel.via.timemanagement;

import cz.cvut.fel.via.kosapirest.Configuration;
import cz.cvut.fel.via.kosapirest.Credentials;
import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.impl.FasadeImpl;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

public class TimeManagementActivity extends Activity {

	static final int NO_SET = 0;
	static final int BAD_NAME = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
	}

	public void submit(View v) {
		String login = ((EditText) findViewById(R.id.username)).getText()
				.toString().trim();
		String pwd = ((EditText) findViewById(R.id.pwd)).getText().toString()
				.trim();

		// TODO odkomentovat :-)
		/*
		 * if (login.equals("") || pwd.equals("")) { showDialog(NO_SET); return;
		 * }
		 */
		TimeManagement.setCredentials(login, pwd);

		RadioGroup faculty = (RadioGroup) findViewById(R.id.radioFaculty);

		int selectedId = faculty.getCheckedRadioButtonId();

		if (selectedId == R.id.radioFEL) {
			TimeManagement.setFel();
		} else if (selectedId == R.id.radioFIT) {
			TimeManagement.setFit();
		}

		

		FasadeImpl fasade = (FasadeImpl) TimeManagement.getFasade();
		if (!fasade.validate()) {
			TimeManagement.deleteFasade();
			showDialog(BAD_NAME);
		} else {
			Intent intent = new Intent(this, StudentActivity.class);
			startActivity(intent);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case BAD_NAME:
			AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
			alertbox.setMessage(R.string.badname);
			alertbox.setNeutralButton("Ok", null);
			return alertbox.show();

		case NO_SET:
			AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
			alertbox1.setMessage(R.string.setUsername);
			alertbox1.setNeutralButton("Ok", null);
			return alertbox1.show();
		}
		return null;
	}
}